<?php
interface transactionTemplate {
	//function authenticate();
	function getErrorMessage();
	function process();
	function setAmount($amount);
	function setBillingInfo($fname, $lname, $address, $city, $state, $zip, $country);
	function setCardInfo($number, $expiration, $cvv="");
	function setTestMode();
}

abstract class transactionBase implements transactionTemplate {
    protected $fname;
    protected $lname;
    protected $address;
    protected $city;
    protected $state;
    protected $zip;
    protected $country;
    protected $cardnumber;
    protected $cvv;
    protected $expiration;
    protected $amount;
    protected $errorMessage;
	protected $transactionType;
	protected $paymentType;
    protected $description;
    protected $transactionID;

	public function debug() {
		echo "<pre>", print_r($this, true), "</pre>";
	}

	public function getErrorMessage() {
		return $this->errorMessage;
	}

	public function getTransactionID() {
		return $this->transactionID;
	}

	public function setTransactionID($transactionID) {
		$this->transactionID = $transactionID;
	}
	
	public function setAmount($amount) {
		$this->amount = number_format($amount, 2, ".", "");
	}

	public function setBillingInfo($fname, $lname, $address, $city, $state, $zip, $country) {
		$this->fname = $fname;
        $this->lname = $lname;
        $this->address = $address;
        $this->city = $city;
        $this->state = $state;
        $this->zip = $zip;
        $this->country = $country;
	}

	public function setCardInfo($number, $expiration, $cvv="") {
		$this->cardnumber = $number;
		$this->expiration = $expiration;
		$this->cvv = $cvv;
	}

	public function setDescription($description) {
		$this->description = $description;
	}

	public function setTransactionType($type) {
		if(empty($this->allowedTransactionTypes) || !is_array($this->allowedTransactionTypes) || in_array($type, $this->allowedTransactionTypes))
			$this->transactionType = $type;
	}

	public function setPaymentType($type)
	{
		if(empty($this->allowedPaymentTypes) || !is_array($this->allowedPaymentTypes) || in_array($type, $this->allowedPaymentTypes))
			$this->paymentType = $type;
	}
}

abstract class transaction {
	private static $allowedTypes = array("authorize", "ezic", "ets");

	public static function init($type, $path="php-transaction") {
		if($path[strlen($path) - 1] == "/")
			$path = substr($path, 0, -1);
		if(in_array($type, self::$allowedTypes) && file_exists($path . "/class." . $type . ".php")) {
			include_once("class." . $type . ".php");
			return new $type();	
		}	
		else {
			throw new Exception("Error: An invalid transaction type was found.  Please use one of the following valid transaction types: " . implode(", ", self::$allowedTypes)); 
		}
	}
}
?>
